#include "cnc_gsk.h"
#include "gsk980\gskapi.h"

#define COMM_TYPE 1
#define TIME_OUT 3000

// 定义文件传输进度回调函数
void OnTransferProgress(unsigned long completeBytes, int percent) {
    // 处理文件传输进度
    std::cout << "Transfer progress: " << percent << "%, " << completeBytes << " bytes transferred." << std::endl;
}

int cnc_list_file_gsk(const char* ip, std::vector<std::string>& fileList, int& fileCount)
{
    int ret;
    // CNC设备的IP地址
    unsigned char cncIPAddr[4];
    // 将字符串IP转换为4字节数组
    if (sscanf(ip, "%hhu.%hhu.%hhu.%hhu", &cncIPAddr[0], &cncIPAddr[1], &cncIPAddr[2], &cncIPAddr[3]) != 4) {
        std::cerr << "Invalid IP address format: " << ip << std::endl;
        fileList.clear();
        fileCount = 0;
        return 1;
    }
    std::cout << "CNC IP address: " << (int)cncIPAddr[0] << "." << (int)cncIPAddr[1] << "." << (int)cncIPAddr[2] << "." << (int)cncIPAddr[3] << std::endl;

    // 创建GSK通信实例
    HINSGSKRM hInst = GSKRM_CreateInstance(cncIPAddr, COMM_TYPE);
    if (!hInst) {
        std::cerr << "Failed to create GSKRM instance" << std::endl;
        fileList.clear();
        fileCount = 0;
        return 1;
    }
    std::cout << "Successfully created GSKRM instance" << std::endl;

    // 设置超时时间(毫秒)
    ret = GSKRM_SetOvertime(hInst, TIME_OUT);
    if (ret != 0) {
        std::cerr << "Failed to set timeout,errcode=0x" << std::hex << ret << std::dec << std::endl;
        GSKRM_CloseInstance(hInst);
        fileList.clear();
        fileCount = 0;
        return 1;
    }
    std::cout << "Successfully connected to CNC device!" << std::endl;

    // 获取文件列表
    const unsigned int bufSize = 1024;
    char fileListBuf[bufSize] = {0};
    unsigned long nameCount = 0;
    
    ret = GSKRM_GetCNCFileList(hInst, fileListBuf, bufSize, &nameCount);
    if (ret != 0) {
        std::cerr << "Failed to get CNC file list,errcode=0x" << std::hex << ret << std::dec << std::endl;
        GSKRM_CloseInstance(hInst);
        fileList.clear();
        fileCount = 0;
        return 1;
    }
    std::cout << "Successfully got CNC file list!" << std::endl;

    // 解析文件列表
    std::vector<std::string> files;
    char* token = strtok(fileListBuf, "|");
    while (token != nullptr) {
        files.push_back(token);
        token = strtok(nullptr, "|");
    }
    std::cout << "Got " << files.size() << " files from CNC device" << std::endl;

    // 显示文件列表
    std::cout << "Available files:" << std::endl;
    for (int i = 0; i < files.size(); ++i) {
        std::cout << i + 1 << ". " << files[i] << std::endl;
    }

    // 设置输出参数
    fileList = files;
    fileCount = files.size();

    // 关闭实例
    GSKRM_CloseInstance(hInst);
    std::cout << "GSKRM instance closed" << std::endl;

    return 0;
}

int cnc_upload_file_gsk(const char* ip, const char* local_path, const char* filename)
{
    int ret;
    // CNC设备的IP地址
    unsigned char cncIPAddr[4];
    // 将字符串IP转换为4字节数组
    if (sscanf(ip, "%hhu.%hhu.%hhu.%hhu", &cncIPAddr[0], &cncIPAddr[1], &cncIPAddr[2], &cncIPAddr[3]) != 4) {
        std::cerr << "Invalid IP address format: " << ip << std::endl;
        return 1;
    }
    std::cout << "CNC IP address: " << (int)cncIPAddr[0] << "." << (int)cncIPAddr[1] << "." << (int)cncIPAddr[2] << "." << (int)cncIPAddr[3] << std::endl;

    // 创建GSK通信实例
    HINSGSKRM hInst = GSKRM_CreateInstance(cncIPAddr, COMM_TYPE);
    if (!hInst) {
        std::cerr << "Failed to create GSKRM instance" << std::endl;
        return 1;
    }
    std::cout << "Successfully created GSKRM instance" << std::endl;

    // 设置超时时间(毫秒)
    ret = GSKRM_SetOvertime(hInst, TIME_OUT);
    if (ret != 0) {
        std::cerr << "Failed to set timeout,errcode=0x" << std::hex << ret << std::dec << std::endl;
        GSKRM_CloseInstance(hInst);
        return 1;
    }
    std::cout << "Successfully connected to CNC device!" << std::endl;

    // 发送文件到CNC
    ret = GSKRM_SendCNCFile(hInst, (char*)local_path, (char*)filename, OnTransferProgress);
    if (ret != 0) {
        std::cerr << "Failed to send file " << filename << " to CNC,errcode=0x" << std::hex << ret << std::dec << std::endl;
        GSKRM_CloseInstance(hInst);
        return 1;
    }
    std::cout << "Successfully sent file " << filename << " to CNC device!" << std::endl;

    // 关闭实例
    GSKRM_CloseInstance(hInst);
    std::cout << "GSKRM instance closed" << std::endl;

    return 0;
}

int cnc_download_file_gsk(const char* ip, const char* local_path, const char* filename)
{
    int ret;
    // CNC设备的IP地址
    unsigned char cncIPAddr[4];
    // 将字符串IP转换为4字节数组
    if (sscanf(ip, "%hhu.%hhu.%hhu.%hhu", &cncIPAddr[0], &cncIPAddr[1], &cncIPAddr[2], &cncIPAddr[3]) != 4) {
        std::cerr << "Invalid IP address format: " << ip << std::endl;
        return 1;
    }
    std::cout << "CNC IP address: " << (int)cncIPAddr[0] << "." << (int)cncIPAddr[1] << "." << (int)cncIPAddr[2] << "." << (int)cncIPAddr[3] << std::endl;

    // 创建GSK通信实例
    HINSGSKRM hInst = GSKRM_CreateInstance(cncIPAddr, COMM_TYPE);
    if (!hInst) {
        std::cerr << "Failed to create GSKRM instance" << std::endl;
        return 1;
    }
    std::cout << "Successfully created GSKRM instance" << std::endl;

    // 设置超时时间(毫秒)
    ret = GSKRM_SetOvertime(hInst, TIME_OUT);
    if (ret != 0) {
        std::cerr << "Failed to set timeout,errcode=0x" << std::hex << ret << std::dec << std::endl;
        GSKRM_CloseInstance(hInst);
        return 1;
    }
    std::cout << "Successfully connected to CNC device!" << std::endl;

    // 下载文件到本地
    ret = GSKRM_ReceiveCNCFile(hInst, (char*)filename, (char*)local_path, OnTransferProgress);
    if (ret != 0) {
        std::cerr << "Failed to download file " << filename << " from CNC,errcode=0x" << std::hex << ret << std::dec << std::endl;
        GSKRM_CloseInstance(hInst);
        return 1;
    }
    std::cout << "Successfully downloaded file " << filename << " from CNC device!" << std::endl;

    // 关闭实例
    GSKRM_CloseInstance(hInst);
    std::cout << "GSKRM instance closed" << std::endl;

    return 0;
}

int cnc_delete_file_gsk(const char* ip, const char* filename)
{
    int ret;
    // CNC设备的IP地址
    unsigned char cncIPAddr[4];
    // 将字符串IP转换为4字节数组
    if (sscanf(ip, "%hhu.%hhu.%hhu.%hhu", &cncIPAddr[0], &cncIPAddr[1], &cncIPAddr[2], &cncIPAddr[3]) != 4) {
        std::cerr << "Invalid IP address format: " << ip << std::endl;
        return 1;
    }
    std::cout << "CNC IP address: " << (int)cncIPAddr[0] << "." << (int)cncIPAddr[1] << "." << (int)cncIPAddr[2] << "." << (int)cncIPAddr[3] << std::endl;

    // 创建GSK通信实例
    HINSGSKRM hInst = GSKRM_CreateInstance(cncIPAddr, COMM_TYPE);
    if (!hInst) {
        std::cerr << "Failed to create GSKRM instance" << std::endl;
        return 1;
    }
    std::cout << "Successfully created GSKRM instance" << std::endl;

    // 设置超时时间(毫秒)
    ret = GSKRM_SetOvertime(hInst, TIME_OUT);
    if (ret != 0) {
        std::cerr << "Failed to set timeout,errcode=0x" << std::hex << ret << std::dec << std::endl;
        GSKRM_CloseInstance(hInst);
        return 1;
    }
    std::cout << "Successfully connected to CNC device!" << std::endl;

    // 删除文件
    ret = GSKRM_DeleteCNCFile(hInst, (char*)filename);
    if (ret != 0) {
        std::cerr << "Failed to delete file " << filename << " from CNC,errcode=0x" << std::hex << ret << std::dec << std::endl;
        GSKRM_CloseInstance(hInst);
        return 1;
    }
    std::cout << "Successfully deleted file " << filename << " from CNC device!" << std::endl;

    // 关闭实例
    GSKRM_CloseInstance(hInst);
    std::cout << "GSKRM instance closed" << std::endl;

    return 0;
}
